package com.wildfire.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wildfire.dao.UserDao;
import com.wildfire.dto.UserDto;
import com.wildfire.model.UserInfo;
import com.wildfire.service.BasicService;
import com.wildfire.service.UserService;
import com.wildfire.vo.*;
import com.wildfire.vo.page.PageResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
@Slf4j
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private BasicService basicService;

    /**
     * create by: zwxu5
     * description: 获取分页列表
     * create time: 2021/11/9 15:58
     *
     * @return com.wildfire.vo.page.PageResult
     * @Param: userQueryReq
     */
    @Override
    public PageResult getUserPage(UserQueryReq userQueryReq) {
        PageHelper.startPage(userQueryReq.getPage(), userQueryReq.getRows());

        List<UserDto> userList = userDao.getUserPage();
        return new PageResult<>(((Page) userList).getTotal(), userList);
    }

    @Override
    public UserInfo getUserInfo(String token) {
       Subject subject = SecurityUtils.getSubject();
       boolean isAuthenticated = subject.isAuthenticated();
       System.out.println("isAuthenticated:" + isAuthenticated);
       UserInfo userInfo = new UserInfo();
       UserDto userDto = userDao.getUserInfo(token);
       userInfo.setAccess(userDao.getUserAccess(userDto.getId()));
       userInfo.setAvator(userDto.getAvator());
       userInfo.setName(userDto.getName());
       userInfo.setId(userDto.getId());
       userInfo.setRouter(JSON.toJSONString(basicService.getAppRouter()));
       return userInfo;
    }

    @Override
    public UserLoginResp userLogin(UserLoginReq userLoginReq) {
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(userLoginReq.getName(),userLoginReq.getPass());
        
        subject.login(usernamePasswordToken);
        System.out.println("login succ");
        boolean isAuthenticated = subject.isAuthenticated();
        System.out.println("isAuthenticated:" + isAuthenticated);
        UserLoginResp userLoginResp = new UserLoginResp();
        //String token = (String) subject.getSession().getId();
        //userLoginResp.setToken(token);
        userLoginResp.setToken(userDao.getUserToken(userLoginReq.getName(), userLoginReq.getPass()));
        //userLoginResp.setRouter(JSON.toJSONString(basicService.getAppRouter()));
        return userLoginResp;
    }

    @Override
    public UserInfo getUserInfoByName(String name) {
        UserInfo userInfo = new UserInfo();
        UserDto userDto = userDao.getUserInfoByName(name);
        userInfo.setAccess(userDao.getUserAccess(userDto.getId()));
        userInfo.setPass(userDto.getPass());
        userInfo.setName(userDto.getName());
        return userInfo;
    }

    @Override
    public void userLogout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
    }

}
